/**
 * Created by Administrator on 2015/1/12.
 */
app.factory("globalVariableService",['$location','$rootScope', 'userService','$modal',function($location,$rootScope,userService ,$modal){
   var variable={};
    variable.userRegisterStep1Profile={};
    variable.userInfo={};
    variable.friendDetailsInfo = {}; /*用在好友列表中某个好友被查看 从列表到好友详情跳转时携带的参数*/
    variable.personSetInfo = {};
    variable.ImageList = [];
    variable.Avatar = {};
    variable.sexConEdu = {};
    variable.provinces = [];
    variable.friendsArray = [];
    variable.userId = 0;
    //我的资料中点击下一步的所有路由
    variable.infoNextRoutes = ['/myInfoDetails/myInfo','/myPersonInfo/myInfo/description',
                '/myPersonInfo/myInfo/description/attestation','/myPersonInfo/myInfo/description/attestation/profile',
                '/'];
    variable.currentInd = 0;//我的资料路由数组中当前路由的索引

    variable.initRootScope=function() {
        $rootScope.isPending=false;
        $rootScope.showBackButton=true;
        $rootScope.canCreateTopic=false;
        $rootScope.showHeader = true;
        $rootScope.showFooter=false;
        $rootScope.glbFooterType = glbFooterType.main;
        $rootScope.showQuit = false;
        $rootScope.removePopover = false;
        $rootScope.addFriends = false;
        $rootScope.chat = false;
        /*$rootScope.acceptApply = false;
        $rootScope.addApply = false;*/
        //我的资料 footer
        $rootScope.nextTxt = '下一步';
        $rootScope.isFirst = false;

    };
    variable.checkLogin=function(toPath){
        var  authToken = getAuthToken();
        if(authToken == null || authToken == "null"){
          /*  $location.path('/user/login').search({toPath:toPath});*/
            $location.path('/user/login').search({backUrl: '/'})
            return false;
        }else {
            userService.isValidatedAutoTken({token: authToken}).then(function (data) {
                if(data.status){
                    setAuthToken(null);
                    /*var modalInstance = $modal.open({
                        templateUrl: 'views/tmpl_modal_alert.html',
                        controller: ModalAlertCtrl,
                        size: 'sm',
                        resolve: {
                            alertObj: function () {
                                return {message: '请重新登录！'};
                            }
                        }
                    });*/
                    $location.path('/user/login').search({toPath:toPath});
                    return false;
                }
            })
        }
        return true;
    };
    variable.isLogin = function () {
        var  authToken = getAuthToken();
        if(authToken == null || authToken == "null"){
            return false;
        }
        return true;
    };
    variable.uploadImageEvent = function (scope) {
        document.removeEventListener("FILE_UPLOADED_EVENT");
        document.addEventListener("FILE_UPLOADED_EVENT", function (evt) {
            $rootScope.isPending = false;
            scope.isUploadSuccess = true;
            scope.uploadedImage = evt.data;
            $rootScope.$$phase || $rootScope.$apply();
            scope.$$phase || scope.$apply();
        }, false);

        document.removeEventListener("FILE_UPLOAD_CANCEL_EVENT");
        document.addEventListener("FILE_UPLOAD_CANCEL_EVENT", function (evt) {
            $rootScope.isPending = false;
            $rootScope.$$phase || $rootScope.$apply();
        }, false);
    };
    variable.uploadImage = function(scope, type){
        $rootScope.isPending = true;
        scope.isUploadSuccess = false;
        //$rootScope.$apply();
        //$scope.$apply();
        var params = {uploadImgType: type};
        if (browser.versions.ios) {
            Log.log("try to call ios native to add image");
            if (window.WebViewJavascriptBridge) {
                WebViewJavascriptBridge.callHandler('uploadImage', params, function (response) {
                    Log.log("call native to upload image");
                });
            }
        } else if (browser.versions.android) {
            Log.log("try to call android native to add image");
            jsb.require('uploadImage', params, function (response) {
                Log.log("call native to upload image");
            });

        }
    };
    variable.userType = 2;
    variable.isCreateTopic = 0;
    variable.consultantClubId = undefined;
    return variable;
}]);
